package com.ruoyi.teach.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiImplicitParam;
import com.ruoyi.teach.domain.TeachBook;
import com.ruoyi.teach.service.ITeachBookService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 教材Controller
 * 
 * @author yepanpan
 * @date 2021-03-15
 */
@Api("教材管理")
@RestController
@RequestMapping("/teach/book")
public class TeachBookController extends BaseController
{
    @Autowired
    private ITeachBookService teachBookService;

    /**
     * 查询参与制作教材列表
     */
    @ApiOperation("查询参与制作教材列表")   
    @PreAuthorize("@ss.hasPermi('teach:book:make')")
    @GetMapping("/make")
    public TableDataInfo make(TeachBook teachBook)
    {
    	teachBook.getParams().put("userId", SecurityUtils.getLoginUser().getUser().getUserId());
        startPage();
        List<TeachBook> list = teachBookService.selectTeachBookList(teachBook);
        return getDataTable(list);
    }
    
    /**
     * 查询自己创建的教材列表
     */
    @ApiOperation("查询自己创建的教材列表")   
    @PreAuthorize("@ss.hasPermi('teach:book:my')")
    @GetMapping("/my")
    public TableDataInfo my(TeachBook teachBook)
    {
    	teachBook.setCreateBy(SecurityUtils.getUsername());
        startPage();
        List<TeachBook> list = teachBookService.selectTeachBookList(teachBook);
        return getDataTable(list);
    }


    /**
     * 查询教材列表
     */
    @ApiOperation("查询教材列表")   
    @PreAuthorize("@ss.hasPermi('teach:book:list')")
    @GetMapping("/list")
    public TableDataInfo list(TeachBook teachBook)
    {
        startPage();
        List<TeachBook> list = teachBookService.selectTeachBookList(teachBook);
        return getDataTable(list);
    }

    
    /**
     * 导出教材列表
     */
    @ApiOperation("导出教材列表")    
    @PreAuthorize("@ss.hasPermi('teach:book:export')")
    @Log(title = "教材", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TeachBook teachBook)
    {
        List<TeachBook> list = teachBookService.selectTeachBookList(teachBook);
        ExcelUtil<TeachBook> util = new ExcelUtil<TeachBook>(TeachBook.class);
        return util.exportExcel(list, "book");
    }

    /**
     * 获取教材详细信息
     */
    @ApiOperation("获取教材详细信息")
    @ApiImplicitParam(name = "id", value = "教材ID", required = true, dataType = "int", paramType = "path") 
    @PreAuthorize("@ss.hasPermi('teach:book:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(teachBookService.selectTeachBookById(id));
    }

    /**
     * 新增教材
     */
    @ApiOperation("新增教材")
    @PreAuthorize("@ss.hasPermi('teach:book:add')")
    @Log(title = "教材", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TeachBook teachBook)
    {
        return toAjax(teachBookService.insertTeachBook(teachBook));
    }

    /**
     * 修改教材
     */
    @ApiOperation("修改教材")  
    @PreAuthorize("@ss.hasPermi('teach:book:edit')")
    @Log(title = "教材", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TeachBook teachBook)
    {
        return toAjax(teachBookService.updateTeachBook(teachBook));
    }

    /**
     * 删除教材
     */
    @ApiOperation("删除教材")
    @ApiImplicitParam(name = "id", value = "教材ID", required = true, dataType = "int", paramType = "path") 
    @PreAuthorize("@ss.hasPermi('teach:book:remove')")
    @Log(title = "教材", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(teachBookService.deleteTeachBookByIds(ids));
    }
}
